package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.ShopMapper;
import com.example.zqh.mapper.UserMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Comment;
import com.example.zqh.mapper.CommentMapper;
import com.example.zqh.pojo.Shop;
import com.example.zqh.pojo.User;
import com.example.zqh.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean addANewComment(Integer shop_id, String comment, Principal principal) {
        AssertUtil.isTrue("".equals(comment),"不能评论空消息");
        //判断商店是否存在
        Shop shop = shopMapper.selectById(shop_id);
        AssertUtil.isTrue(shop == null,"参数传递错误");

        String name = principal.getName();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));

        Comment comment1 = new Comment();
        comment1.setShopId(shop.getId())
                .setUserId(user.getId())
                .setUsernam(user.getName())
                .setUserpic(user.getHeadUrl())
                .setContent(comment)
                .setCreateTime(new Date())
                .setUp(0);
        int i = commentMapper.insert(comment1);
        AssertUtil.isTrue(i!=1,"添加评论失败");
        return RespBean.success("添加评论成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询评论
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean queryComments(Integer shop_id) {
        List<Comment> comments = commentMapper.selectList(new QueryWrapper<Comment>().eq("shop_id", shop_id));
        return RespBean.success("查询成功",comments);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 删除评论
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean deleteComment(Integer id) {
        int i = commentMapper.deleteById(id);
        AssertUtil.isTrue(i!=1,"评论删除失败");
        return RespBean.success("评论删除成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 评论点赞
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean commentLike(Integer id) {
        Comment comment = commentMapper.selectById(id);
        AssertUtil.isTrue(comment == null,"参数传递错误");
        comment.setUp(comment.getUp() + 1);
        int i = commentMapper.updateById(comment);
        AssertUtil.isTrue(i != 1,"点赞失败");
        return RespBean.success("点赞成功");
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 获取某个店铺评论的数量
    */
    @Override
    @Transactional(readOnly = true)
    public RespBean getCommentNumber(Integer shop_id) {
        Integer number = commentMapper.getTheNumberOfReviewsForAStore(shop_id);
        return RespBean.success("查询成功",number);
    }

    @Override
    public RespBean getAllNumberComment() {
        return RespBean.success("查询成功",commentMapper.getAllNumber());
    }
}
